/* 
 *	Copyright (C) 2009-2010 miaohua
 *	http://code.google.com/p/lrcshower/
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */
// ProgressDlg.cpp : implementation file
//

#include "stdafx.h"
#include <string>
#include "TypeDef.h"
#include "AppMsgDef.h"
#include "SongProp.h"
#include "MusicBox.h"
#include "ProgressDlg.h"
#include "PlayListDlg.h"
#include "HelperFunctions.h"


// CProgressDlg dialog

IMPLEMENT_DYNAMIC(CProgressDlg, CDialog)

CProgressDlg::CProgressDlg(const CString & Folder, bool IsRecurse, CDialog * ParentDlg, CWnd* pParent /*=NULL*/)
	: CDialog(CProgressDlg::IDD, pParent)
    , m_Exit(false)
    , m_Folder(Folder)
    , m_IsRecurse(IsRecurse)
    , m_ThreadHdler(NULL)
    , m_ParentDlg(ParentDlg)
{

}

CProgressDlg::~CProgressDlg()
{    
     if( m_ThreadHdler )
     { 
         WaitForSingleObject(m_ThreadHdler, INFINITE);
         CloseHandle(m_ThreadHdler);
     }
}

void CProgressDlg::DoDataExchange(CDataExchange* pDX)
{
	 CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CProgressDlg, CDialog)
     ON_BN_CLICKED(IDCANCEL, &CProgressDlg::OnBnClickedCancel)
     ON_WM_CLOSE()
     ON_MESSAGE(WM_EXIT, OnDlgExit)
END_MESSAGE_MAP()


// CProgressDlg message handlers
BOOL CProgressDlg::OnInitDialog()
{
     CDialog::OnInitDialog();

     unsigned int ID ;
     m_ThreadHdler = (HANDLE)_beginthreadex(NULL,0,ThreadOnAddFolder,this,0,&ID);

     return TRUE;
}

LRESULT CProgressDlg::OnDlgExit(WPARAM wParam, LPARAM lParam)
{
     EndDialog(IDCANCEL);
     return 1;
}

void CProgressDlg::OnBnClickedCancel()
{
     m_Exit = true;
}

void CProgressDlg::UpdateProg(const CString & FilePath, int Num)
{
     GetDlgItem(IDC_CURFILE)->SetWindowText(FilePath);
     GetDlgItem(IDC_FILE_NUM)->SetWindowText(IntToStr(Num).c_str());
}

void CProgressDlg::OnClose()
{
     return;
}

unsigned int WINAPI CProgressDlg::ThreadOnAddFolder(void* Param)
{
     CProgressDlg * Self = ((CProgressDlg*)Param);
     Self->OnAddFolder();
     return 0;
}

void CProgressDlg::AddFolderHelper(const CString & Path)
{
     WIN32_FIND_DATA FindFileData;
     TCHAR DirPath[MAX_PATH];
     _tcscpy_s(DirPath, MAX_PATH, (LPCTSTR)Path);
     _tcscat_s(DirPath, MAX_PATH, _T("\\*.*"));

     HANDLE Finder = FindFirstFile(DirPath, &FindFileData);
     if( Finder == INVALID_HANDLE_VALUE ) return;
    
     int Counter = 0;
     do{
          if( m_Exit ) break;

          // Get full path
          CString FullPath = m_Folder + _T("\\") + FindFileData.cFileName;

          if( (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && m_IsRecurse )
              AddFolderHelper(FullPath);

          // Check wether it is a audio media file
          if( SongProp::CheckAudioFile((LPCTSTR)FullPath) )
          {
              // Insert into play list(both gui and song list)
              ((CPlayListDlg*)m_ParentDlg)->InsertOneSong(FullPath);
              // Update gui info
              ++Counter;
              UpdateProg(FullPath,Counter);
          }
     }while(FindNextFile(Finder,&FindFileData));

     // Close finder
     FindClose(Finder);

}

void CProgressDlg::OnAddFolder()
{
     // Do add
     AddFolderHelper(m_Folder);
     // Post exit message
     PostMessage(WM_EXIT, 0, 0);
}

